package com.hspedu.socketExercise;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author DL5O
 * @version 1.0
 */
public class Client_ {
    public static void main(String[] args) throws IOException {
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        Socket socket = new Socket(hostAddress, 9999);//参数，主机域名或地址 + 对应服务的端口号
        System.out.println("连接服务器成功");
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello，server".getBytes());
        //需要设置结束标记
        socket.shutdownOutput();

        //获取和socket相关联的输入，
        InputStream inputStream = socket.getInputStream();
        int readLen = 0;
        byte[] buf = new byte[50];
        while((readLen = inputStream.read(buf))!= -1){
            System.out.println(new String(buf,0,readLen));
        }
        socket.shutdownInput();

        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端退出...");
    }
}
